const contain = {
  videourl:[
    {url:"https://cloud.video.taobao.com//play/u/2794371653/p/1/e/6/t/1/300279199883.mp4",id:'1'},
    {url:"https://cloud.video.taobao.com//play/u/2794371653/p/1/e/6/t/1/298669909725.mp4",id:'2'},
    {url:"https://cloud.video.taobao.com//play/u/2794371653/p/1/e/6/t/1/299344649525.mp4",id:'3'},
    {url:"https://cloud.video.taobao.com//play/u/2794371653/p/1/e/6/t/1/299011696401.mp4",id:'4'},
    {url:"https://cloud.video.taobao.com//play/u/2794371653/p/1/e/6/t/1/300279219837.mp4",id:'5'},
    {url:"https://cloud.video.taobao.com//play/u/2794371653/p/1/e/6/t/1/299345165724.mp4",id:'6'},
    {url:"https://cloud.video.taobao.com//play/u/2794371653/p/1/e/6/t/1/299954283445.mp4",id:'7'},
    {url:"https://cloud.video.taobao.com//play/u/2794371653/p/1/e/6/t/1/299642130054.mp4",id:'8'},
  ],
  videopost:[
    {
     url:"https://img.alicdn.com/imgextra/i1/4161056916/O1CN01UrYRlB20xZqwRk6Bw_!!4161056916.jpg",
     goodsurl:"https://img.alicdn.com/imgextra/i2/4161056916/O1CN015F5drN20xZqijdOBN_!!4161056916.jpg",
     title:"[热巴同款]Swisse斯维诗葡萄籽仔粉胶囊精华原花青素维生素c 淡斑",
     id: "527036505839"
    },
    {
     url:"https://img.alicdn.com/imgextra/i1/4161056916/O1CN01HDXun720xZqrafDup_!!4161056916.jpg",
     goodsurl:"https://img.alicdn.com/imgextra/i2/4161056916/O1CN01gIPq8s20xZqnT9Eol_!!4161056916.jpg",
     title:"[热巴同款]Swisse斯维诗胶原水光片口服玻尿酸胶原蛋白辅助抗糖vc",
     id:"574576989687"
    },
    {
      url:"https://img.alicdn.com/imgextra/i1/4161056916/O1CN01mg1EI620xZqrILZck_!!4161056916.jpg",
      goodsurl:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01C29s6l20xZqrbtMBD_!!4161056916.jpg",
      title:"姐姐同款Swisse斯维诗辅酶q10软胶囊150mg50粒 CoQ10保护心脏健康",
      id: "527048692450"
    },
    {
      url:"https://img.alicdn.com/imgextra/i3/4161056916/O1CN013bmvJM20xZqnS8CLI_!!4161056916.jpg",
      goodsurl:"https://img.alicdn.com/imgextra/i1/4161056916/O1CN01uM86TW20xZqsbJxs6_!!4161056916.jpg",
      title:"姐姐同款澳洲Swisse斯维诗奶蓟草片120粒 加班熬夜解酒护常备旰片",
      id: "527050980299"
    },
    {
      url:"https://img.alicdn.com/imgextra/i3/4161056916/O1CN015xuyWI20xZqokkxac_!!4161056916.jpg",
      goodsurl:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01fKjwnn20xZqsKPj3P_!!4161056916.jpg",
      title:"澳洲进口Swisse斯维诗男士复合维生素120片番茄红素维生素C添能量",
      id: "527048328080"
    },
    {
      url:"https://img.alicdn.com/imgextra/i2/4161056916/O1CN01LhMzsv20xZqsaEh5o_!!4161056916.jpg",
      goodsurl:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01hC6oxI20xZqxNcEYp_!!4161056916.jpg",
      title:"[姐姐同款]Swisse斯维诗金装抗糖丸抵抗糖化清自由基胶原焕活美肌",
      id:"618199527842"
    },
    {
      url:"https://img.alicdn.com/imgextra/i2/4161056916/O1CN01IbULao20xZqnSDEbT_!!4161056916.jpg",
      goodsurl:"https://img.alicdn.com/imgextra/i2/4161056916/O1CN01gIPq8s20xZqnT9Eol_!!4161056916.jpg",
      title:"[热巴同款]Swisse斯维诗胶原水光片口服玻尿酸胶原蛋白辅助抗糖vc",
      id:"574576989687"
    },
    {
      url:"https://img.alicdn.com/imgextra/i3/4161056916/O1CN01ZUd05F20xZqupT72y_!!4161056916.jpg",
      goodsurl:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01baXHPL20xZqikjfOY_!!4161056916.jpg",
      title:"姐姐同款Swisse斯维诗深海鱼油软胶囊欧米伽3omega3中老年 DHA",
      id:"527049104768"
    }
  ]
}
Page({
  data: {
    ...contain,
    idx:0,
    playTime:'',
    a:1,
    dialogflag:false,//控制对话框的标志位
		flag:false,
		current_energy:0

	},
	
	//请求能量值
	reqEnergy(){
		getApp().cloud.function.invoke('client',{},"getCurrentUserInfo").then(res => {
			console.log('当前用户信息===》',res); 

			if(res.code != 0){
				my.alert({
					content: res.msg
				})

				return;
			}

			this.setData({
				current_energy:res.data.current_energy,
				mineInfo:res.data
			})



		})  
	},

	//检查视频是否观看完毕
	checkVideoEnd(){
		let videourl = this.data.videourl

		getApp().cloud.function.invoke('client',{},"totalVideoStatus").then(res => {
			console.log('查询视频今日是否已观看完毕===》',res); 
			console.log('查询视频今日是否已观看完毕===》',res.data.length); 
			console.log('查询视频今日是否已观看完毕===》',res.data); 
			console.log('查询视频今日是否已观看完毕===》',typeof(res.data)); 

			//视频链接  -- 因为数据类型居然分开了，就只能对应拿了
			let noSee = []
			let endSee = []

			//视频对应的商品链接
			let sNoSee = []
			let sEndSee = []

			if(res.data.length == 0){
				videourl.map((v,i) => {
					v.end = false
					v.time=0
				})

				this.setData({
					videourl
				})
			}else{
				videourl.map((v,i) => {
					
					res.data.map((item,idx) => {
						// console.info(item)

						console.info(v.id == item.remark)

						if(v.id == item.remark){
							v.end = true
							v.time = 15
						}

					})

					
					
					
				})

				console.info(videourl)

				// this.setData({
				// 	videourl
				// })

				// console.info(noSee.concat(endSee))
				// console.info(sNoSee.concat(sEndSee))

				videourl.map((v,i) => {
					if(v.end == true && v.time == 15){
						sEndSee.push(this.data.videopost[i])

						console.info(this.data.videopost[i])

						endSee.push(v)
					}else{
						noSee.push(v)
						sNoSee.push(this.data.videopost[i])
						console.info(this.data.videopost[i])
					}
				})


				this.setData({
					videourl:noSee.concat(endSee),
					videopost:sNoSee.concat(sEndSee)
				})
			}

		})
			
	},

	onShow(){
		this.reqEnergy()
		this.checkVideoEnd()
	},

  onLoad() {
    this.videourl = this.data.videourl.map((v,i)=>{
      if(v.time) {
        return v
      } else {
        v.time = 0
        return v
      }
    })
    console.log(this.data.videopost[0]);
     this.setData({
      videoUrl: this.videoUrl,
       flag:false
      
    })
    console.log(this.data.flag);
    
     let videoUrl = this.data.videourl
   
      videoUrl.filter((v,i)=>{
        if(v.time>15){
          videoUrl.push(v)
          videoUrl.splice(i,1)

          
        }
      })
      this.setData({
        videoUrl:videoUrl
      }),
      console.log(this.data.videopost[this.data.idx].id);
      
    
  },
	change(e){
		console.log(e.detail.current);

		my.hideToast()
		// clearInterval(this.playTime)
		this.setData({
			idx:e.detail.current
		})
	},
	play(){
		
		clearInterval(this.playTime)
	
			this.playTime = setInterval(()=>{

				let lin = this.data.videourl

				if(lin[this.data.idx].time >= 15){

					getApp().cloud.function.invoke('client',{nick_name:this.data.mineInfo.nick_name,videoId:lin[this.data.idx].id},"video").then(res => {
						console.log('视频已观看完毕===》',res); 

						if(res.code != 0){
							my.showToast({
								content:res.msg
							});
							return
						}

						lin[this.data.idx].end = true

						my.showToast({
							content:'恭喜您已完成视频观看\n获得1次游戏机会！',
							duration:3000
						});

						this.setData({
							videourl: lin
						})

					})



					clearInterval(this.playTime)
					return
				}

				

				lin[this.data.idx].time += 1

				this.setData({
					videourl: lin
				})
				
			},1000)
		// console.log(time);
	},
	pause() {
		clearInterval(this.playTime)
		console.log(this.data.videourl);
		
	},
	cha(){
		this.setData({
			dialogflag:true,
			flag:true
		})
		
	},
	close() {
		this.setData({
			dialogflag:false
		})
	},
	buy() {
		console.log(11);


		my.tb.openDetail ({
			itemId: this.data.videopost[this.data.idx].id,
			success: (res) => {
				
			}
		});

	},
	Loading() {
		
	},
});
